import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

/* Layout */
import Layout from '@/layout'

/* Router Modules */
import componentsRouter from './modules/components'
import tableRouter from '@/router/modules/table'
import nestedRouter from '@/router/modules/nested'
import chartsRouter from '@/router/modules/charts'
import backupsRouter from '@/router/modules/backups'

/**
 * Note: sub-menu only appear when route children.length >= 1
 * Detail see: https://panjiachen.github.io/vue-element-admin-site/guide/essentials/router-and-nav.html
 *
 * hidden: true                   if set true, item will not show in the sidebar(default is false)
 * alwaysShow: true               if set true, will always show the root menu
 *                                if not set alwaysShow, when item has more than one children route,
 *                                it will becomes nested mode, otherwise not show the root menu
 * redirect: noRedirect           if set noRedirect will no redirect in the breadcrumb
 * name:'router-name'             the name is used by <keep-alive> (must set!!!)
 * meta : {
 roles: ['admin','editor']    control the page roles (you can set multiple roles)
 title: 'title'               the name show in sidebar and breadcrumb (recommend set)
 icon: 'svg-name'/'el-icon-x' the icon show in the sidebar
 noCache: true                if set true, the page will no be cached(default is false)
 affix: true                  if set true, the tag will affix in the tags-view
 breadcrumb: false            if set false, the item will hidden in breadcrumb(default is true)
 activeMenu: '/example/list'  if set path, the sidebar will highlight the path you set
 }
 */

/**
 * constantRoutes
 * a base page that does not have permission requirements
 * all roles can be accessed
 */
export const constantRoutes = [
  { // 帖子管理
    path: '/blogManager',
    component: Layout,
    redirect: '/blogManager/index',
    hidden: true,
    children: [
      {
        path: 'index',
        component: () => import('@/views/blogManager/index'),
        name: 'BlogManager',
        meta: { title: 'Home', noCache: true, roles: ['管理员'], icon: 'tree-table' }
      }
    ]
  },
  {
    path: '/redirect',
    component: Layout,
    hidden: true,
    children: [
      {
        path: '/redirect/:path(.*)',
        component: () => import('@/views/redirect/index')
      }
    ]
  },
  {
    path: '/login',
    component: () => import('@/views/login/index'),
    hidden: true
  },
  {
    path: '/viewerHome',
    component: () => import('@/views/viewerHome/index'),
    hidden: true
  },
  {
    path: '/toUserHome/:id',
    name: 'toUserHome', // 这里定义了路由名称
    meta: { title: 'User Home', icon: 'user', noCache: true },
    component: () => import('@/views/userHome/toUserHome/index'),
    hidden: true
  },
  {
    path: '/auth-redirect',
    component: () => import('@/views/login/auth-redirect'),
    hidden: true
  },
  {
    path: '/404',
    component: () => import('@/views/error-page/404'),
    hidden: true
  },
  {
    path: '/401',
    component: () => import('@/views/error-page/401'),
    hidden: true
  },
  {
    path: '/',
    component: Layout,
    redirect: '/dashboard',
    children: [
      {
        path: 'dashboard',
        component: () => import('@/views/blogManager/index'),
        name: 'Dashboard',
        meta: { title: 'dashboard', icon: 'dashboard', affix: true }
      }
    ]
  },
  {
    path: '/addpost',
    component: Layout,
    children: [
      {
        path: 'index',
        component: () => import('@/views/addpost/index'),
        name: 'Addpost',
        meta: { title: 'addpost', icon: 'el-icon-edit' }
      }
    ]
  },
  // {
  //   path: '/documentation',
  //   component: Layout,
  //   children: [
  //     {
  //       path: 'index',
  //       component: () => import('@/views/documentation/index'),
  //       name: 'Documentation',
  //       meta: { title: 'documentation', icon: 'documentation', affix: true }
  //     }
  //   ]
  // },
  // {
  //   path: '/guide',
  //   component: Layout,
  //   redirect: '/guide/index',
  //   children: [
  //     {
  //       path: 'index',
  //       component: () => import('@/views/guide/index'),
  //       name: 'Guide',
  //       meta: { title: 'guide', icon: 'guide', noCache: true }
  //     }
  //   ]
  // },
  {
    path: '/profile',
    component: Layout,
    redirect: '/profile/index',
    hidden: true,
    children: [
      {
        path: 'index',
        component: () => import('@/views/profile/index'),
        name: 'Profile',
        meta: { title: 'profile', icon: 'user', noCache: true }
      }
    ]
  },
  {
    // 注册
    path: '/register',
    component: () => import('@/views/register/index'),
    hidden: true
  },
  {
    // 个人主页
    path: '/userHome',
    component: Layout,
    redirect: '/userHome/index',
    meta: {
      title: 'userHome',
      icon: 'user'
    },
    children: [
      {
        path: '/index',
        component: () => import('@/views/userHome/index'),
        name: 'UserHome',
        meta: { title: 'userHome', noCache: true, icon: 'user' }
      }
    //   {
    //     // 个人帖子
    //     path: '/myBlog',
    //     component: () => import('@/views/userHome/myBlog'),
    //     name: 'MyBlog',
    //     meta: { title: 'MyBlog', noCache: true, icon: 'list' }
    //   },
    //   {
    //     // 点赞帖子
    //     path: '/likeBlog',
    //     component: () => import('@/views/userHome/likeBlog'),
    //     name: 'LikeBlog',
    //     meta: { title: 'LikeBlog', noCache: true, icon: 'el-icon-thumb' }
    //   },
    //   {
    //     // 收藏帖子
    //     path: '/collectBlog',
    //     component: () => import('@/views/userHome/collectBlog'),
    //     name: 'CollectBlog',
    //     meta: { title: 'CollectBlog', noCache: true, icon: 'star' }
    //   }
    ]
  },
  { // 聊天室
    path: '/chatManager',
    component: Layout,
    redirect: '/chatManager/index',
    meta: { title: 'chatManager', icon: 'wechat' },
    children: [
      {
        path: 'index',
        component: () => import('@/views/chatManager/index'),
        name: 'ChatManager',
        meta: { title: 'ChatManager', icon: 'wechat', noCache: true }
      }
    ]
  }

]

/**
 * asyncRoutes
 * the routes that need to be dynamically loaded based on user roles
 */
export const asyncRoutes = [
  // {
  //   path: '/permission',
  //   component: Layout,
  //   redirect: '/permission/page',
  //   alwaysShow: true, // will always show the root menu
  //   name: 'Permission',
  //   meta: {
  //     title: 'permission',
  //     icon: 'lock',
  //     roles: ['普通用户', '管理员'] // you can set roles in root nav
  //   },
  //   children: [
  //     {
  //       path: 'page',
  //       component: () => import('@/views/permission/page'),
  //       name: 'PagePermission',
  //       meta: {
  //         title: 'Page Permission',
  //         roles: ['管理员'] // or you can only set roles in sub nav
  //       }
  //     },
  //     {
  //       path: 'directive',
  //       component: () => import('@/views/permission/directive'),
  //       name: 'DirectivePermission',
  //       meta: {
  //         title: 'Directive Permission',
  //         roles: ['管理员']
  //         // if do not set roles, means: this page does not require permission
  //       }
  //     },
  //     {
  //       path: 'role',
  //       component: () => import('@/views/permission/role'),
  //       name: 'RolePermission',
  //       meta: {
  //         title: 'Role Permission',
  //         roles: ['admin']
  //       }
  //     }
  //   ]
  // },

  // {
  //   path: '/icon',
  //   component: Layout,
  //   children: [
  //     {
  //       path: 'index',
  //       component: () => import('@/views/icons/index'),
  //       name: 'Icons',
  //       meta: { title: 'Icons', icon: 'icon', noCache: true }
  //     }
  //   ]
  // },

  /** when your routing map is too long, you can split it into small modules **/
  // componentsRouter,
  // chartsRouter,
  // nestedRouter,
  // tableRouter,
  // backupsRouter,

  {
    // 用户管理
    path: '/userManager',
    component: Layout,
    redirect: '/roleManager/index',
    children: [
      {
        path: 'index',
        component: () => import('@/views/roleManager/index'),
        name: 'RoleManager',
        meta: { title: 'userManager', noCache: true, roles: ['管理员'], icon: 'peoples' }
      }
    ]
  },
  {
    // 审核管理
    path: '/auditBlog',
    component: Layout,
    redirect: '/auditBlog/index',
    children: [
      {
        path: 'index',
        component: () => import('@/views/auditBlog/index'),
        name: 'AuditBlog',
        meta: { title: 'AuditBlog', noCache: true, roles: ['管理员'], icon: 'tree-table' }
      }
    ]
  },
  // {
  //   path: 'external-link',
  //   component: Layout,
  //   children: [
  //     {
  //       path: 'https://github.com/PanJiaChen/vue-element-admin',
  //       meta: { title: 'External Link', icon: 'link' }
  //     }
  //   ]
  // },

  // 404 page must be placed at the end !!!
  { path: '*', redirect: '/404', hidden: true }
]

const createRouter = () => new Router({
  // mode: 'history', // require service support
  scrollBehavior: () => ({ y: 0 }),
  routes: constantRoutes
})

const router = createRouter()

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
}

export default router
